package com.ting.tadmin.util.Alipay;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

public class AlipayUtil {
    private static AlipayClient alipayClient;
    @Autowired
    private static AlipayConfig alipayConfig;

    static {
//        alipayClient  = new DefaultAlipayClient(
//                alipayConfig.getURL()
//                ,alipayConfig.getAppid()
//                ,alipayConfig.getAppprivatekey()
//                ,alipayConfig.getFormat()
//                ,alipayConfig.getCharset()
//                ,alipayConfig.getAlipaypublickey()
//                ,alipayConfig.getSigntype());
        alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
                "2016082600315935",
                "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCCINm+w/CAsVTH1MomoXL4O3DoNn7O5mwwWPTfUKaXAj7M1q0PPgMTfEnJEesYVbEK8BmaFFCmZ6CiAKmhbJQdJLN3eW9gtmhtwmDYPvnN/wLp9OMhikouDfM5fPQG1G/oqfwywWTJWTq06J3I2jFT1p5KmuVZQxsUxWkrGAfeR/udnORK9OMaLjhB5Ym9/6IMoGkLjgyWxYbk4Uu01xu1LlO/v0EFv5rKlvH9K3SyUBChg8LNOTK1tVs72shid3pFU2Xr4ipWyW1stx7eatpZj0FzaKEWQ+5n/YAwixMJSNwXOqjYIus/tQW/OQbs1TP1ucwoVKEsCi0/bFcK1swLAgMBAAECggEAMMSA9kZWbtSGknGD5SBihvc35D7wHTgr2Z6s/rgqBrP3tyrJJso1uuq8eFd3xOHwfMnovl7YrtRtVelrqSyZAacMfz0KabNSxbewNbq8QeBNRkLZkGtUS5g8zBGxHpIk8owqERd7Xx5Kqxv2bhyfmez327vbMi29C3bEaDsUT9AfevFTwrWJu+zwaFfwHxYOcj1nPHwqL9LyvsNdb4T4h0z9lJmkUPnv7KgrgGGb89FQ+BPgfm3s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3DN2jPPGdDvXZ1eG0ZAzeyKXzWQi/p2ohvacKlRAFPbcRz/r/vp0LMlgB9lQsXXtWRgRv7p7DP5r4ErnxFkxokdz0sR8U6l98w8Ys0/zhK9yiBlVQrnqcViH/wRF8nCBkTtRI4hIeFZq0KoQ0tZnPcJkQ/vQ033qN8BKrsYCYj0Ch4oo7Njy2rKVy4azCpS4hb8P6h28ok36L0ySliPuPvQmxSQ0dpsxhhko0opPDOEFpOJxUfLaMESNtqUsZmg9ZCrlcEChy7vDOOE6IWSOCYpBi0ARNOrtfQQs8bQbM2kZgs7abJd+LPEwIDAQAB",
                "RSA2");
    }
    public String webpay(JSONObject params) throws Exception{
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl("http://app2127414121test.mapp-test.xyz/alipay/return");
        alipayRequest.setNotifyUrl("http://app2127414121test.mapp-test.xyz/alipay/notify");
        alipayRequest.setBizContent(params.toString());
       return alipayClient.pageExecute(alipayRequest).getBody();
    }

    public boolean notify(Map<String,String> params) throws Exception{
        return AlipaySignature.rsaCheckV1(params,alipayConfig.getAlipaypublickey(),alipayConfig.getCharset());

    }
}
